package cn.matezk.lianlian.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.matezk.lianlian.dao.ProductDao;
import cn.matezk.lianlian.model.Product;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.iotexplorer.v20190423.IotexplorerClient;
import com.tencentcloudapi.iotexplorer.v20190423.models.GetStudioProductListRequest;
import com.tencentcloudapi.iotexplorer.v20190423.models.GetStudioProductListResponse;
import com.tencentcloudapi.iotexplorer.v20190423.models.ProductEntry;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class ProductSyncTask {
    @Value("${tencent.endpoint}")
    private String endpoint;
    @Value("${tencent.region}")
    private String region;
    @Value("${tencent.secret-id}")
    private String secretId;
    @Value("${tencent.secret-key}")
    private String secretKey;
    @Autowired
    private ProductDao productDao;

    @XxlJob("productSyncTask")
    @Transactional
    public void execute() throws TencentCloudSDKException, InterruptedException {
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(endpoint);
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        Credential credential = new Credential(secretId, secretKey);
        GetStudioProductListRequest request = new GetStudioProductListRequest();

        IotexplorerClient client = new IotexplorerClient(credential, region, clientProfile);
        GetStudioProductListResponse response = client.GetStudioProductList(request);
        ProductEntry[] products = response.getProducts();
        for (ProductEntry tp : products) {
            Product product = productDao.selectByPrimaryKey(tp.getProductId());
            if (ObjectUtil.isNull(product)) {
                product = new Product();
                product.setPlatform(Product.PLATFORM_TENCENT);
                product.setProductId(tp.getProductId());
                product.setProductName(tp.getProductName());
                product.setProductDesc(tp.getProductDesc());
                product.setStatus(tp.getDevStatus());
                product.setCreateTime(DateUtil.date(tp.getCreateTime() * 1000));
                productDao.insertSelective(product);
            } else {
                product.setProductName(tp.getProductName());
                product.setProductDesc(tp.getProductDesc());
                product.setStatus(tp.getDevStatus());
                productDao.updateByPrimaryKeySelective(product);
            }
        }
    }
}
